﻿using B.S.BasicData.Api.Read.Application.Command.Approval;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using Common;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.Approval
{
    public class GetApprovalHandler : IRequestHandler<GetApprovalCommand, ApiPaging<ApprovalDTO>>
    {
        private readonly IBaseRepository<ApprovalModel> approvalRepo;
        private readonly IBaseRepository<DepartmentModel> departmentRepo;
        private readonly IBaseRepository<UserModel> userRepo;

        public GetApprovalHandler(IBaseRepository<ApprovalModel> approvalRepo,IBaseRepository<DepartmentModel> departmentRepo,IBaseRepository<UserModel> userRepo)
        {
            this.approvalRepo = approvalRepo;
            this.departmentRepo = departmentRepo;
            this.userRepo = userRepo;
        }
        public Task<ApiPaging<ApprovalDTO>> Handle(GetApprovalCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<ApprovalDTO> res=new ApiPaging<ApprovalDTO>();
            var approval = approvalRepo.GetAll().Where(x => x.IsDelete == false);
            var department = departmentRepo.GetAll();
            var user = userRepo.GetAll();
            var isboosid = departmentRepo.GetAll().Where(x => x.Uid.Equals(request.Uid)).Select(x => x.Id).ToList();
            if (isboosid.Any())
            {
                approval = approval.Where(x => isboosid.Contains(x.Bid));
            }
            else
            {
                approval = approval.Where(x => x.Uid.Equals(request.Uid));
            }
            if (request.LeaveStatrTime!=null)
            {
                approval = approval.Where(x => x.LeaveStatrTime >= request.LeaveStatrTime.Value);
            }
            if (request.LeaveEndTime!=null)
            {
                approval = approval.Where(x => x.LeaveEndTime <= request.LeaveEndTime.Value);
            }
            if (request.ApprovalState!=null)
            {
                approval = approval.Where(x => x.ApprovalState == request.ApprovalState);
            }
            if (!string.IsNullOrEmpty(request.Name))
            {
                approval = approval.Where(x => x.Name.Contains(request.Name));
            }
            var linq = from a in approval
                       join b in department on a.Bid equals b.Id
                       join c in user on b.Uid equals c.Id
                       select new ApprovalDTO
                       {
                           Name = a.Name,
                           BuFuName = c.UserName,
                           ApprocalYuan = a.ApprocalYuan,
                           ApprovalState = a.ApprovalState,
                           BaodaoTime = a.BaodaoTime,
                           Bid = a.Bid,
                           Bname = b.DepartmentName,
                           LeaveStatrTime = a.LeaveStatrTime,
                           LeaveEndTime=a.LeaveEndTime,
                           QingYuan = a.QingYuan,
                           TypeId = a.TypeId,
                           Id = a.Id,
                           CreateTime = a.CreateTime,
                            CountHours = a.CountHours,
                            
                       };
            res.Msg = "查询审批信息成功";
            res.Code = ApiEnum.Success;
            res.TotalCount=linq.Count();
            res.TotalPage=(int)Math.Ceiling(res.TotalCount*1.0)/request.PageSize;
            res.Datas = linq.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return Task.FromResult(res);
        }
    }
}
